package com.bookmarket.controller;

import com.bookmarket.entity.Book;
import com.bookmarket.entity.Custom;
import com.bookmarket.entity.Shop;
import com.bookmarket.entity.Stype;
import com.bookmarket.service.CzgChaxunService;
import com.bookmarket.service.IProductListService;
import com.github.pagehelper.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/pc")
@SessionAttributes(names="list")
public class LJProductController {

    @Resource
    private IProductListService iProductListService;

    @Resource
    private CzgChaxunService czgChaxunService;

    //查询出版社
    @RequestMapping("/chaPublish")
    public ModelAndView chaPublish(){
        ModelAndView mv=new ModelAndView();
        List<Book> list=iProductListService.selectByPublish();
        mv.addObject("list",list);
        mv.setView(new MappingJackson2JsonView());
        return mv;
    }
    //查询作者
    @RequestMapping("/chaauthor")
    public  ModelAndView chaauthor(){
        ModelAndView mv=new ModelAndView();
        List<Book> list=iProductListService.selectByauthor();
        mv.addObject("list",list);
        mv.setView(new MappingJackson2JsonView());
        return mv;
    }
    @RequestMapping("/chaunzhi")
    public ModelAndView chaunzhi(int btypeId){
        ModelAndView mv=new ModelAndView();
        mv.addObject("btypeId",btypeId);
        mv.setViewName("stage/LJproduct_list");
        return mv;
    }
    @RequestMapping("/cz")
    public ModelAndView cz(int stypeId){
        ModelAndView mv=new ModelAndView();
        mv.addObject("stypeId",stypeId);
        mv.setViewName("stage/LJproduct_list");
        return mv;
    }
    //分页语句查(取代查询全部;包含模糊搜索;排序)
    @RequestMapping("/getByPage")
    public Map getByPage(int pageNum,int pageSize,Book book,String sales,String rate,String product,String stypeId,String btypeId) {
        if("".equals(sales) && "".equals(rate) && "".equals(product)){
            if(book.getPrice()==0 && "".equals(book.getAuthor())&&"".equals(book.getPublish())){
                if("".equals(btypeId)){
                    String id=stypeId+"";
                    String [] stypeid={id};
                    Page page=iProductListService.selectByStype(pageNum,pageSize,stypeid);
                    Map map = new HashMap();
                    map.put("list", page);
                    map.put("totalPage", page.getPages());
                    return map;
                }else if("".equals(stypeId)){
                    Map map = new HashMap();
                    //查询大类型对应的小类型
                    String [] stypeid=null;
                    String shuzu="";
                    List<Stype> list=iProductListService.selectByBtypeId(Integer.parseInt(btypeId));
                    for (Stype s:list){
                        shuzu+=Integer.toString(s.getId())+",";
                        stypeid=shuzu.split(",");
                    }
                    Page page=iProductListService.selectByStype(pageNum,pageSize,stypeid);
                    map.put("list", page);
                    map.put("totalPage", page.getPages());
                    return map;
                }else{
                    Page page=iProductListService.selectBook(pageNum,pageSize);
                    Map map=new HashMap();
                    map.put("list", page);
                    map.put("totalPage", page.getPages());
                    return map;
                }
            }else{
                Page page =iProductListService.getPage(pageNum,pageSize,book);
                Map map = new HashMap();
                map.put("list", page);
                map.put("totalPage", page.getPages());
                return map;
            }
        } else{
            Map m=new HashMap();
            m.put("sales",sales);
            m.put("rate",rate);
            m.put("product",product);
            Page page =iProductListService.OrderBy(pageNum,pageSize,m);
            Map map = new HashMap();
            map.put("list", page);
            map.put("totalPage", page.getPages());
            return map;
        }
    }
    //评论
    @RequestMapping("/chaMess")
    public ModelAndView chaMess(int id){
        ModelAndView mv=new ModelAndView();
        int shuliang=iProductListService.Count(id);
        mv.addObject("MessList",shuliang);
        mv.setView(new MappingJackson2JsonView());
        return mv;
    }
    //加入购物车
    @RequestMapping("/insertShop")
    public ModelAndView insertShop(int id,String name){
        //根据id查图书
        Book b=iProductListService.selectById(id);
        //根据name查用户
        Custom c=iProductListService.selectByName(name);
        //时间转换
        //SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        //System.out.println(df.format(new Date()));// new Date()为获取当前系统时间
        //向购物车类中set属性
        Shop s=new Shop();
        s.setCreateTime(new Date());
        s.setBook_picture(b.getPicture());
        s.setBook_price(b.getPrice());
        s.setBook_name(b.getName());
        s.setBook_num(1);
        s.setBook(b);
        s.setCustom(c);



        ModelAndView mv=new ModelAndView();
        Boolean flag=iProductListService.insertShop(s);
        mv.addObject("flag",flag);
        mv.setView(new MappingJackson2JsonView());
        return mv;
    }
    //热门浏览
    @RequestMapping("/chaClick")
    public  ModelAndView chaClick(){
        ModelAndView mv=new ModelAndView();
        List<Book> list=iProductListService.selectByclickBook();
        mv.addObject("list",list);
        mv.setView(new MappingJackson2JsonView());
        return mv;
    }
    public IProductListService getiProductListService() {
        return iProductListService;
    }

    public void setiProductListService(IProductListService iProductListService) {
        this.iProductListService = iProductListService;
    }
}
